import Controller from "@/Controller";
import { ITask } from "@/Controller/State/task";
import React from "react";

export const TaskContext = React.createContext<ITask>(undefined as never)

export function useTask() {

    const task = React.useContext(TaskContext);

    const delTask = React.useCallback(() => {
        Controller.instance.run(async s => {
            const p = s.taskInfo.list.indexOf(task);
            s.taskInfo.list.splice(p, 1);
            s.taskInfo.list = [...s.taskInfo.list];
        })
    }, [task])

    const hideTask = React.useCallback(() => {
        Controller.instance.run(async s => {
            task.minimize = true;
            s.taskInfo.list = [...s.taskInfo.list];
        })
    }, [task])

    const showTask = React.useCallback(() => {
        Controller.instance.run(async s => {
            task.minimize = false;
            s.taskInfo.list = [...s.taskInfo.list];
        })
    }, [task])

    return {
        /** 当前任务 */
        task,

        /** 删除任务 */
        delTask,

        /** 隐藏任务 */
        hideTask,

        /** 显示任务 */
        showTask,
    }
}